from PyQt4 import QtCore, QtGui, Qt
import notes_editor

class NoteWindow(QtGui.QMainWindow):
    '''
    A dialog widget used for the indexing window. Subclassed for its close event.
    '''
    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self, parent)
        self.note_dialog = notes_editor.Ui_MainWindow()
        self.note_dialog.setupUi(self)
        self.strikeFont = QtGui.QFont(self)
        self.connect(self.note_dialog.actionSave, QtCore.SIGNAL('triggered()'), self.save_note)
        self.connect(self.note_dialog.actionItalic, QtCore.SIGNAL('triggered()'), self.italic_note)
        self.connect(self.note_dialog.actionAlignRight, QtCore.SIGNAL('triggered()'), self.align_right_note)
        self.connect(self.note_dialog.actionAlignLeft, QtCore.SIGNAL('triggered()'), self.align_left_note)
        self.connect(self.note_dialog.actionAlignCenter, QtCore.SIGNAL('triggered()'), self.align_center_note)
        self.connect(self.note_dialog.actionAlignJustify, QtCore.SIGNAL('triggered()'),
                     self.align_justify_note)
        self.connect(self.note_dialog.actionBulletedList,
                     QtCore.SIGNAL('triggered()'), self.bulleted_list_note)
        self.connect(self.note_dialog.actionNumberedList, QtCore.SIGNAL('triggered()'),
                     self.numbered_list_note)
        self.connect(self.note_dialog.actionRemoveFormat, QtCore.SIGNAL('triggered()'),
                     self.remove_format_note)
        self.connect(self.note_dialog.actionBold, QtCore.SIGNAL('triggered()'), self.bold_note)
        self.connect(self.note_dialog.actionStrike, QtCore.SIGNAL('triggered()'), self.strike_note)
        self.connect(self.note_dialog.actionSuperscript, QtCore.SIGNAL('triggered()'), self.superscript_note)
        self.connect(self.note_dialog.actionSubscript, QtCore.SIGNAL('triggered()'), self.subscript_note)
        self.connect(self.note_dialog.actionColor, QtCore.SIGNAL('triggered()'), self.color_note)
        self.connect(self.note_dialog.actionFont, QtCore.SIGNAL('triggered()'), self.font_note)
        self.connect(self.note_dialog.actionBGColor, QtCore.SIGNAL('triggered()'), self.bgcolor_note)
        self.connect(self.note_dialog.actionUnderline, QtCore.SIGNAL('triggered()'), self.underline_note)
        self.connect(self.note_dialog.textEdit, QtCore.SIGNAL('textChanged()'), self.note_changed)
        self.connect(self.note_dialog.actionUnderline, QtCore.SIGNAL('triggered()'), self.underline_note)
        self.connect(self.note_dialog.actionUndo, QtCore.SIGNAL('triggered()'), self.undo_note)
        self.connect(self.note_dialog.actionRedo, QtCore.SIGNAL('triggered()'), self.redo_note)
        
        
        self.noteTextChanged = False
        self.note_dialog.actionSave.setChecked(True)

    def initialize(self, config_dict):
        self.config_dict = config_dict
        
    def closeEvent(self, event):
        self.save_note()
        self.emit(QtCore.SIGNAL('windowClosed'))

    def update_note_html(self, cite):
        self.citeId = cite['EID']
        try:
            clip_html = open('%s%s.nxt' % (self.config_dict['storageLocation'], self.citeId)).read()
        except:
            output = ['<b> Notes: </b>']
            title = cite['Title']
            authors = cite['Authors']
            journal = cite['Journal']
            yr = cite['Year']            
            issue = cite['Issue']
            pages = cite['Pages']
            output_val = '%s, <i>%s</i>, <b><font color="#330000">%s</font></b>. %s; %s: %s.' % (
                authors, title, journal, yr, issue, pages)
            output += [output_val]
            clip_html = '<br>'.join(output)
        self.note_dialog.textEdit.setHtml(clip_html)
        self.note_dialog.textEdit.autoFormatting()
        self.note_dialog.actionSave.setChecked(True)
        self.note_dialog.textEdit.moveCursor(QtGui.QTextCursor.End)
        
    def save_note(self):
        saveTextVal = self.note_dialog.textEdit.toHtml()
        try:
            f = open('%s%s.nxt' % (self.config_dict['storageLocation'], self.citeId), 'w')
            f.write(saveTextVal)
            f.close()
            self.noteTextChanged = False
            self.note_dialog.actionSave.setChecked(True)
        except:
            pass

    def bold_note(self):
        if self.note_dialog.textEdit.fontWeight() == 50:
            self.note_dialog.textEdit.setFontWeight(75)
        else:
            self.note_dialog.textEdit.setFontWeight(50)
        self.note_dialog.textEdit.moveCursor(QtGui.QTextCursor.WordRight)

    def italic_note(self):
        if self.note_dialog.textEdit.fontItalic():
            self.note_dialog.textEdit.setFontItalic(False)
        else:
            self.note_dialog.textEdit.setFontItalic(True)
        self.note_dialog.textEdit.moveCursor(QtGui.QTextCursor.WordRight)

    def color_note(self):
        col = QtGui.QColorDialog.getColor()
        if col.isValid():
            self.note_dialog.textEdit.setTextColor(col)
        self.note_dialog.textEdit.moveCursor(QtGui.QTextCursor.WordRight)
        
    def bgcolor_note(self):
        col = QtGui.QColorDialog.getColor()
        if col.isValid():
            self.note_dialog.textEdit.setTextBackgroundColor(col)
        self.note_dialog.textEdit.moveCursor(QtGui.QTextCursor.WordRight)

    def font_note(self):
        col = QtGui.QFontDialog.getFont()[0]
        self.note_dialog.textEdit.setCurrentFont(col)
        self.note_dialog.textEdit.moveCursor(QtGui.QTextCursor.WordRight)

    def superscript_note(self):
        cur = self.note_dialog.textEdit.textCursor()
        if (not cur.hasSelection()):
            cur.insertHtml('<sup></sup>')
        else:
            cur.insertHtml('<sup>%s</sup>' % (cur.selectedText()))

    def subscript_note(self):
        cur = self.note_dialog.textEdit.textCursor()
        if (not cur.hasSelection()):
            cur.insertHtml('<sub></sub>')
        else:
            cur.insertHtml('<sub>%s</sub>' % (cur.selectedText()))
        self.note_dialog.textEdit.moveCursor(QtGui.QTextCursor.WordRight)
        
    def underline_note(self):
        if self.note_dialog.textEdit.fontUnderline():
            self.note_dialog.textEdit.setFontUnderline(False)
        else:
            self.note_dialog.textEdit.setFontUnderline(True)     

    def strike_note(self):
        print type(self.strikeFont)
        if self.note_dialog.textEdit.currentFont().strikeOut():
            self.strikeFont.setStrikeOut(False)
        else:
            self.strikeFont.setStrikeOut(True)
        self.note_dialog.textEdit.setCurrentFont(self.strikeFont)
        self.note_dialog.textEdit.moveCursor(QtGui.QTextCursor.WordRight)

    def undo_note(self):
        self.note_dialog.textEdit.undo()

    def redo_note(self):
        self.note_dialog.textEdit.redo()
        
    def note_changed(self):
        self.note_dialog.actionSave.setChecked(False)
        self.noteTextChanged = True

    def align_right_note(self):
        self.note_dialog.textEdit.setAlignment(QtCore.Qt.AlignRight)

    def align_left_note(self):
        self.note_dialog.textEdit.setAlignment(QtCore.Qt.AlignLeft)

    def align_center_note(self):
        self.note_dialog.textEdit.setAlignment(QtCore.Qt.AlignCenter)

    def align_justify_note(self):
        self.note_dialog.textEdit.setAlignment(QtCore.Qt.AlignJustify)        

    def bulleted_list_note(self):
        cur = self.note_dialog.textEdit.textCursor()
        if (not cur.hasSelection()):
            cur.insertHtml('<ul></ul>')
        else:
            textVal = cur.selectedText()
            textVal = textVal.split("\n")
            textVal = ["<li>%s</li>" % x for x in textVal]
            textVal = "\n".join(textVal)
            print textVal
            cur.insertHtml('<ul>%s</ul>' % textVal)
        self.note_dialog.textEdit.moveCursor(QtGui.QTextCursor.WordRight)

    def numbered_list_note(self):
        cur = self.note_dialog.textEdit.textCursor()
        if (not cur.hasSelection()):
            cur.insertHtml('<ol></ol>')
        else:
            textVal = cur.selectedText()
            textVal = textVal.split("\n")
            textVal = ["<li>%s</li>" % x for x in textVal]
            textVal = "\n".join(textVal)
            print textVal
            cur.insertHtml('<ol>%s</ol>' % textVal)
        self.note_dialog.textEdit.moveCursor(QtGui.QTextCursor.WordRight)

    def remove_format_note(self):
        cur = self.note_dialog.textEdit.textCursor()
        if (not cur.hasSelection()):
            cur.insertHtml('')
        else:
            cur.insertHtml('%s' % (cur.selectedText()))

